#coding=utf-8
import threading
import json
import sys
import queue
import socket
import time
import requests

httpUrl = "http://118.26.23.141:20092/InterWebServer"

testnum = 30
mode = 1

def appClientThread(user,pwd,id,crtq):
    while 1:
        try:
            if crtq.qsize() > 0:
                cmd = crtq.get(block=False)
                print(cmd)
                if cmd == '2':#系统撤防
                    sdata = {
                        'Cmd':"SetAlarmState",
                        'LoginId':user,
                        'LoginPsw':pwd,
                        'DeviceId':id,
                        'DevicePwd':id,
                        'Operate':str(cmd),
                        'userdata':''
                    }
                    print(sdata)
                    sdata = json.dumps(sdata)
                    ack = requests.post(httpUrl,data=sdata)
                    print(ack.text,ack.status_code)
                elif cmd == '1':#系统在家布防
                    sdata = {
                        'Cmd':"SetAlarmState",
                        'LoginId':user,
                        'LoginPsw':pwd,
                        'DeviceId':id,
                        'DevicePwd':id,
                        'Operate':str(cmd),
                        'userdata':''
                    }
                    print(sdata)
                    sdata = json.dumps(sdata)
                    ack = requests.post(httpUrl,data=sdata)
                    print(ack.text,ack.status_code)
                    pass
                elif cmd == '0':
                    sdata = {
                        'Cmd':"SetAlarmState",
                        'LoginId':user,
                        'LoginPsw':pwd,
                        'DeviceId':id,
                        'DevicePwd':id,
                        'Operate':str(cmd),
                        'userdata':''
                    }
                    print(sdata)
                    sdata = json.dumps(sdata)
                    ack = requests.post(httpUrl,data=sdata)
                    print(ack.text,ack.status_code)
            else:
                time.sleep(0.01)
        except BaseException:
            pass

def controlRecvTthread(crtq):
    cmd = 1
    while True:
        try:
            #cmd = input("请输入命令>>:")
            cmd = int(cmd)
            cmd += 1
            cmd %= 3
            if cmd == 3:
                cmd = 0
            indata = int(cmd)
            cnt = 0
            for cq in crtq:
                if mode == 1:
                    indata = int(cmd)
                    cnt += 1
                    if int(cnt) > int(testnum/3) and int(cnt) <= int(testnum/3*2):
                        indata = int(indata) + 1
                        indata = int(indata) % 3
                    elif int(cnt) <= int(testnum/3):
                        pass
                    else:
                        #print(type(indata),indata)
                        indata = int(indata) + 2
                        indata = int(indata) % 3

                indata = str(indata)
                #print(indata,cnt)
                cq.put(indata)
            time.sleep(1000)
        except BaseException:
            pass


def createUser(id,crtq):

    th1 = threading.Thread(target=appClientThread,args=("develop","123456",id,crtq))
    th1.start()

if __name__ == "__main__":
    ControlQ = []
    file = open("111.data","r")
    for line in range(0,testnum):
        id = file.readline(9)
        #print("line:",line,id)
        id = id.replace("\n","")
        cq = queue.Queue(100)
        createUser(id,cq)
        ControlQ.append(cq)
    file.close()

    thread = threading.Thread(target=controlRecvTthread,args=(ControlQ,))
    thread.start()

